<script setup>
import { reactive, ref, toRefs, onMounted } from 'vue';
import { Search } from '@element-plus/icons-vue';
import Pagination from '@/components/Pagination/index.vue';
import { getqueryBomPageList } from '@/server/api/biz.js';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
});

const emits = defineEmits(['update:modelValue', 'close', 'selected']);

const currentRow = ref(null);
const list = ref([]); // 已下单订单列表
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fileName: undefined,
  },
});
const { queryParams } = toRefs(data);
const radio = ref();

onMounted(() => {
  getList();
});

const onClose = () => {
  emits('update:modelValue', false);
};

/**
 * 获取已下单订单列表
 */
const getList = async () => {
  const res = await getqueryBomPageList({
    ...data.queryParams,
  });
  if (res.code === 200) {
    if (Array.isArray(res.data.records) && res.data.records.length > 0) {
      list.value = res.data.records.map((item) => ({
        id: item.id,
        fileName: item.fileName,
        quantity: item.quantity,
        privateInventory: item.bomMatchList != null ? item.bomMatchList[2] : 0,
        order: item.bomMatchList != null ? item.bomMatchList[1] : 0,
        mailed: item.bomMatchList != null ? item.bomMatchList[3] : 0,
        createTime: item.createTime,
        fileSerial: item.fileSerial,
        quantity: item.quantity,
      }));
    } else {
      list.value = [];
    }

    total.value = res.data.total;
  } else {
    ElMessage.error(res.msg);
  }
};

/**
 * 数据处理
 * @param {*} data
 */
const dataProcessing = (data) => {
  const arr = [];
  data.records.forEach((item) => {
    arr.push({
      id: item.id,
      fileName: item.fileName,
      quantity: item.quantity,
      privateInventory: item.bomMatchList != null ? item.bomMatchList[2] : 0,
      order: item.bomMatchList != null ? item.bomMatchList[1] : 0,
      mailed: item.bomMatchList != null ? item.bomMatchList[3] : 0,
      createTime: item.createTime,
      fileSerial: item.fileSerial,
      quantity: item.quantity,
    });
  });
  bomData.total = Number(data.total);
  bomData.size = Number(data.size);
  bomData.current = Number(data.current);
  bomData.data = arr;
};
// 查询
const handleQuery = () => {
  getList();
};

let fileId = ref();
const handleCurrentChange = (row) => {
  currentRow.value = row;
  fileId.value = row?.id;
};

/**
 * 确认选择
 */
const handleSubmit = () => {
  if (!currentRow.value) {
    return ElMessage.warning(t('mall.select_an_order'));
  }
  onClose();
  emits('close');
  emits('selected', currentRow.value);
};
</script>

<template>
  <div class="select-modal">
    <!-- 创建渠道弹窗 -->
    <el-dialog
      v-model="props.modelValue"
      :title="$t('smt.SelectBOM')"
      center
      :before-close="onClose"
      :close-on-click-modal="false"
    >
      <el-row type="flex" justify="end">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item prop="fileName" style="margin-bottom: 12px">
            <el-input
              v-model="queryParams.fileName"
              :placeholder="t('smt.BOMFile')"
              clearable
              style="width: 254px"
              @keyup.enter="handleQuery"
            >
              <template #append>
                <el-icon @click="handleQuery" class="icon"><Search /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table :data="list" border style="width: 100%" @current-change="handleCurrentChange">
        <el-table-column width="50px" align="center">
          <template #default="scope">
            <el-radio v-model="radio" :label="scope.row.id">
              <span></span>
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="fileName" :label="$t('smt.BOMInformation')" width="">
          <template #default="scope">
            <div class="f-12">《{{ scope.row.fileName }}》</div>
            <div class="f-12">{{ scope.row.createTime }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" :label="$t('smt.Numberofpatches')" />
        <el-table-column prop="address" :label="$t('smt.PreparedComponents')">
          <el-table-column prop="privateInventory" :label="$t('smt.Privatestock')">
          </el-table-column>
          <!-- <el-table-column prop="address" label="IBE现货"> </el-table-column> -->
          <el-table-column prop="order" :label="$t('smt.IBEorderrequired')"> </el-table-column>
          <el-table-column prop="mailed" :label="$t('smt.Tobesent')"> </el-table-column>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <template #footer>
        <span class="dialog-footer">
          <el-button palin @click="onClose">{{ $t('smt.Cancel') }}</el-button>
          <el-button type="primary" @click="handleSubmit()"> {{ $t('smt.Define') }} </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.icon {
  cursor: pointer;
}
</style>
